# -*- coding: utf-8 -*-

import os
import sys

start_master_file = [
    '# -*- coding: utf-8 -*-\n',
    'from gevent import monkey; monkey.patch_os()\n',
    '\n',
    'if __name__=="__main__":\n',
    '    from zaneapp.master.master import Master\n',
    '    master = Master()\n',
    "    master.config('config.json','appmain.py')\n",
    '    master.start()\n',
    '    \n',
    '    '
]

config_file = [
    '{\n',
    '    "master":{"roothost":"localhost","rootport":9999,"webport":9998},\n',
    '    "servers":{\n',
    '        "net":{\n',
    '             "netport":1000,\n',
    '             "name":"gate",\n',
    '             "remoteport":[{"rootport":20001,"rootname":"gate"}],\n',
    '             "app":"app.apptest"\n',
    '        },\n',
    '        "gate":{"rootport":20001,"name":"gate"}\n',
    '    },\n',
    '    "db":{\n',
    '        "host":"localhost",\n',
    '        "user":"root",\n',
    '        "passwd":"111",\n',
    '        "port":3306,\n',
    '        "db":"test",\n',
    '        "charset":"utf8"\n',
    '    },\n',
    '    "memcached":{\n',
    '        "urls":["127.0.0.1:11211"],\n',
    '        "hostname":"test"\n',
    '    }\n',
    '}\n'
]

app_main_file = [
    '# -*- coding: utf-8 -*-\n',
    '\n',
    'from gevent import monkey; \n',
    'monkey.patch_os()\n',
    '\n',
    "import json,sys\n",
    'from zaneapp.server.server import FFServer\n',
    '\n',
    'if __name__=="__main__":\n',
    '    args = sys.argv\n',
    '    servername = None\n',
    '    config = None\n',
    '    if len(args)>2:\n',
    '        servername = args[1]\n',
    "        config = json.load(open(args[2],'r'))\n",
    '    else:\n',
    '        raise ValueError\n',
    "    dbconf = config.get('db')\n",
    "    memconf = config.get('memcached')\n",
    "    sersconf = config.get('servers',{})\n",
    "    masterconf = config.get('master',{})\n",
    '    serconfig = sersconf.get(servername)\n',
    '    ser = FFServer()\n',
    '    ser.config(serconfig, servername=servername, dbconfig=dbconf, memconfig=memconf, masterconf=masterconf)\n',
    '    ser.start()\n',
    '    \n',
    '    '
]

app_test_file = [
    '# -*- coding: utf-8 -*-\n',
    '\n',
    'from zaneapp.server.globalobject import netserviceHandle\n',
    '\n',
    '@netserviceHandle\n',
    'def echo_1(_conn,data):\n',
    '    return data\n',
    '    \n',
    '    '
]

client_file = [
    '# -*- coding: utf-8 -*-\n',
    '\n',
    'import time\n',
    '\n',
    'from socket import AF_INET,SOCK_STREAM,socket\n',
    'from thread import start_new\n',
    'import struct\n',
    "HOST='localhost'\n",
    'PORT=1000\n',
    'BUFSIZE=1024\n',
    'ADDR=(HOST , PORT)\n',
    'client = socket(AF_INET,SOCK_STREAM)\n',
    'client.connect(ADDR)\n',
    '\n',
    'def sendData(sendstr,commandId):\n',
    '    HEAD_0 = chr(0)\n',
    '    HEAD_1 = chr(0)\n',
    '    HEAD_2 = chr(0)\n',
    '    HEAD_3 = chr(0)\n',
    '    ProtoVersion = chr(0)\n',
    '    ServerVersion = 0\n',
    '    sendstr = sendstr\n',
    "    data = struct.pack('!sssss3I',HEAD_0,HEAD_1,HEAD_2,\\\n",
    '                       HEAD_3,ProtoVersion,ServerVersion,\\\n',
    '                       len(sendstr)+4,commandId)\n',
    '    senddata = data+sendstr\n',
    '    return senddata\n',
    '\n',
    'def resolveRecvdata(data):\n',
    "    head = struct.unpack('!sssss3I',data[:17])\n",
    '    length = head[6]\n',
    '    data = data[17:17+length]\n',
    '    return data\n',
    '\n',
    's1 = time.time()\n',
    '\n',
    'def start():\n',
    '    while True:\n',
    '        time.sleep(1)\n',
    "        client.sendall(sendData('asdfe',1))\n",
    '\n',
    'start()',
    '\n',
    '    '
]


def create_file(root_path, path, file_content):
    _file = open(root_path + '/' + path, 'w')
    _file.writelines(file_content)
    _file.close()


def execute(*args):
    if not args:
        sys.stdout.write("command error \n")
    project_name = args[0]
    sys.stdout.write("create dir %s \n" % project_name)
    root_path = project_name
    os.mkdir(root_path)
    create_file(root_path, 'startmaster.py', start_master_file)
    create_file(root_path, 'config.json', config_file)
    create_file(root_path, 'appmain.py', app_main_file)

    root_path = str(project_name) + '/' + 'app'
    os.mkdir(root_path)
    create_file(root_path, '__init__.py', [])
    create_file(root_path, 'apptest.py', app_test_file)

    root_path = str(project_name) + '/' + 'tool'
    os.mkdir(root_path)
    create_file(root_path, '__init__.py', [])
    create_file(root_path, 'clienttest.py', client_file)

    sys.stdout.write("create success \n")

